/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.jobmesmanage.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.jobmesmanage.entity.SchoStuwork;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.SchoStuworkService;
import com.lczyfz.campusinfo.modules.sys.entity.User;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.jobmesmanage.entity.StuJob;
import com.lczyfz.campusinfo.modules.jobmesmanage.service.StuJobService;

import java.util.List;

/**
 * 报名勤工Controller
 * @author zms
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${adminPath}/jobmesmanage/stuJob")
public class StuJobController extends BaseController {

	@Autowired
	private StuJobService stuJobService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private SchoStuworkService schoStuworkService;
	@ModelAttribute
	public StuJob get(@RequestParam(required=false) String id) {
		StuJob entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = stuJobService.get(id);
		}
		if (entity == null){
			entity = new StuJob();
		}
		return entity;
	}
	
	@RequiresPermissions("jobmesmanage:stuJob:view")
	@RequestMapping(value = {"list", ""})
	public String list(StuJob stuJob, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StuJob> page = stuJobService.findPage(new Page<StuJob>(request, response), stuJob);
		List<StuJob> stuJobs = stuJobService.findList(stuJob);
		model.addAttribute("stuJobs",stuJobs);
		model.addAttribute("page", page);
		return "modules/jobmesmanage/stuJobList";
	}
	@RequiresPermissions("jobmesmanage:stuJob:view")
	@RequestMapping(value = "detail")
	public String detail(StuJob stuJob, HttpServletRequest request, HttpServletResponse response, Model model) {
		Integer stujobid = Integer.valueOf(stuJob.getJobId());
		stuJob.setJobId(stujobid.toString());
		Page<StuJob> page = stuJobService.findPage(new Page<StuJob>(request, response), stuJob);
		List<StuJob> stuJobs = stuJobService.findList(stuJob);
		model.addAttribute("stuJobs",stuJobs);
		model.addAttribute("page", page);
		return "modules/jobmesmanage/stuJobList";
	}
	@RequiresPermissions("jobmesmanage:stuJob:view")
	@RequestMapping(value = "form")
	public String form(StuJob stuJob, Model model) {
		SchoStuwork schoStuwork = new SchoStuwork();
		List<SchoStuwork> schoStuworks = schoStuworkService.findList(schoStuwork);
		User campusinfoUser = new User();
		List<User> campusinfoUseres = systemService.findUser(campusinfoUser);
		model.addAttribute("campusinfoUseres", campusinfoUseres);
		model.addAttribute("schoStuworks", schoStuworks);
		return "modules/jobmesmanage/stuJobForm";
	}

	@RequiresPermissions("jobmesmanage:stuJob:edit")
	@RequestMapping(value = "save")
	public String save(StuJob stuJob, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stuJob)){
			return form(stuJob, model);
		}
		stuJobService.save(stuJob);
		addMessage(redirectAttributes, "保存报名勤工成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/stuJob/?repage";
	}
	
	@RequiresPermissions("jobmesmanage:stuJob:edit")
	@RequestMapping(value = "delete")
	public String delete(StuJob stuJob, RedirectAttributes redirectAttributes) {
		stuJobService.delete(stuJob);
		addMessage(redirectAttributes, "删除报名勤工成功");
		return "redirect:"+Global.getAdminPath()+"/jobmesmanage/stuJob/?repage";
	}

}